const {query} = require("../../plugins/db");
const uuid = require("node-uuid");
const {PED_SALT,EXPIRE_SIN,PEIVATE_KEY} = require("../../plugins/config");
const jwt = require("jsonwebtoken");
let data;
const s = {
    "type":"SUCCESS",
    "msg":"操作成功"
}
const e = {
    "type":"error",
    "msg":"操作失败"
}
//添加消息
let setNotice = async function(user_from,user_to,nickname,content_id,content_name,action,router){
    let notice = {
        notice_id:uuid.v1(),//消息id
        user_from:user_from,//发起者
        nickname:nickname,
        user_to:user_to,//接收者
        content_name:content_name,
        action:action,//动作
        content_id:content_id,//内容id
        router:router,//路由
        createtime:Date.now()-8*60*60,
        state:0
    }
    let sql = "insert into notice set ?"
    const result = await query(sql,notice);
}
//获取求助列表
async function webGetHelpList(req,res) {
    console.log(req.body);
    let sql1 = "select count(*) as count from help where (ispublic=1 or ispublic=0)";
    if (req.body.lable != "") sql1 = `${sql1} and help_lable="${req.body.lable}"`;//有分类时
    if (req.body.tag != "") sql1 = `${sql1} and help_tag like '%${req.body.tag}%'`;//标签时
    let info1 = []
    const counts = await query(sql1, info1);
    let count = counts[0].count;
    let pagesize = (req.body.pagesize *= 1);
    let page = (req.body.page - 1) * pagesize;
    let info = [pagesize, page];
    let sql = "select help.help_id,help.help_title,help.createtime,help.help_read_num, user.nickname from help,user where help.user_id=user.user_id";
    if (req.body.lable != '') sql = `${sql} and help.help_lable="${req.body.lable}"`;//有分类时
    if (req.body.tag != '') sql = `${sql} and help.help_tag like '%${req.body.tag}%'`;//标签时
    sql = `${sql} and (help.ispublic=0 or help.ispublic=1)`;
    sql = `${sql} order by help.help_read_num desc limit ? offset ?`;
    const result = await query(sql, info);
    data = {
        state: s,
        data: result,
        count: count
    }
    // console.log(data)
    // console.log(result)
    res.send(data);
}
//求助详情
async function webGetHelpContent(req,res){
    let info = [req.body.id];
    let sql = "select * from help,user where help.user_id=user.user_id and help_id=?";
    const result = await query(sql,info);
    await query("update help set help_read_num=help_read_num+1 where help_id=?",info);
    if(result.length===0){
        data={
            state:e,
            data:{}
        }
    }else {
        data={
            state:s,
            data:result[0]
        }
    }
    //console.log(result);
    res.send(data);
}
//获取文章列表
async function webGetArticleList(req,res) {
    let sql1 = "select count(*) as count from article where (ispublic=0 or ispublic=1)";
    if (req.body.lable != "") sql1 = `${sql1} and article_lable='${req.body.lable}'`;//有分类时
    let info1 = []
    const counts = await query(sql1, info1);
    let count = counts[0].count;
    let pagesize = (req.body.pagesize *= 1);
    let page = (req.body.page - 1) * pagesize;
    let info = [pagesize, page];
    let sql = "select article.article_read_num,article.article_id,article.article_title,article.article_introduction," +
        "article.article_createtime,user.nickname from article,user where article.user_id=user.user_id"
    if (req.body.lable != '') sql = `${sql} and article.article_lable='${req.body.lable}'`;//有分类时
    sql = `${sql} and (article.ispublic=0 or article.ispublic=1)`;
    sql = `${sql} order by article.article_read_num desc limit ? offset ?`;
    const result = await query(sql, info);
    data = {
        state: s,
        data: result,
        count: count
    }
    // console.log(data)
    // console.log(result)
    res.send(data);
}
//文章详情
async function webGetArticleContent(req,res){
    let info = [req.body.id];
    let sql = "select * from article,user where article.user_id=user.user_id and article_id=?";
    const result = await query(sql,info);
    await query("update article set article_read_num=article_read_num+1 where article_id=?",info);
    if(result.length===0){
        data={
            state:e,
            data:{}
        }
    }else {
        data={
            state:s,
            data:result[0]
        }
    }
    res.send(data);
}
//获取公司列表
async function webGetCompanyList(req,res) {
    let sql1 = "select count(*) as count from company";
    let info1 = [];
    const counts = await query(sql1, info1);
    let count = counts[0].count;
    let pagesize = (req.body.pagesize *= 1);
    let page = (req.body.page - 1) * pagesize;
    let info = [pagesize, page];
    let sql = "select company_id,company_name from company order by company_read_num desc limit ? offset ?"
    const result = await query(sql, info);
    data = {
        state: s,
        data: result,
        count: count
    }
    // console.log(data)
    // console.log(result)
    res.send(data);
}
//公司详情
async function webGetCompanyContent(req,res){
    let info = [req.body.id];
    let sql = "select * from job where company_id=?";
    let sql1 = "select * from company where company_id=?"
    const jobList = await query(sql,info);
    const company = await query(sql1,info);
    await query("update company set company_read_num=company_read_num+1 where company_id=?",info);
    if(company.length===0){
        data={
            state:e,
            data:{}
        }
    }else {
        data={
            state:s,
            data: {
                joblist:jobList,
                company:company[0]
            }
        }
    }
    res.send(data);
}
//获取工作列表
async function webGetJobList(req,res) {
    let sql1 = "select count(*) as count from job where (ispublic=0 or ispublic=1)";
    if (req.body.lable != "") sql1 = `${sql1} and job_lable='${req.body.lable}'`;//有分类时
    let info1 = []
    const counts = await query(sql1, info1);
    let count = counts[0].count;
    let pagesize = (req.body.pagesize *= 1);
    let page = (req.body.page - 1) * pagesize;
    let info = [pagesize, page];
    let sql = "select job.job_id,job.job_name,job.job_createtime,job.job_salary,job.job_read_num,company.company_name" +
        " from job,company where job.company_id=company.company_id"
    if (req.body.lable != '') sql = `${sql} and job.job_lable='${req.body.lable}'`;//有分类时
    sql = `${sql} and (job.ispublic=0 or job.ispublic=1)`;
    sql = `${sql} order by job.job_read_num desc limit ? offset ?`;
    const result = await query(sql, info);
    data = {
        state: s,
        data: result,
        count: count
    }
    // console.log(data)
    // console.log(result)
    res.send(data);
}
//工作详情
async function webGetJobContent(req,res){
    let info = [req.body.id];
    let sql = "select * from job,company where job.company_id=company.company_id and job_id=?";
    const result = await query(sql,info);
    await query("update job set job_read_num=job_read_num+1 where job_id=?",info);
    if(result.length===0){
        data={
            state:e,
            data:{}
        }
    }else {
        data={
            state:s,
            data: result[0]
        }
    }
    res.send(data);
}
//评论
async function webSetComment(req,res){
    let time = Date.now()-8*60*60;
    setNotice(req.user.uid,req.body.user_to,req.user.nickname,req.body.content_id,req.body.content_name,"评论了你",req.body.router);
    let info = {
        comment_id:uuid.v1(),//评论id
        user_id:req.user.uid,//用户id
        content_id:req.body.content_id,//内容id
        comment_content:req.body.comment_content,//内容
        comment_createtime:time,//创建时间
        comment_state:0,//状态
        comment_istop:0,//是否置顶
        ispublic:0,//是否显示
    }
    let sql = "insert into comment set ?";
    const result = await query(sql,info);
    data={
        state:s,
        data: {}
        }
    res.send(data);
}
//获取评论列表列表
async function webGetCommentList(req,res) {
    let info = [req.body.content_id]
    let sql1 = "select count(*) as count from comment where content_id=?";
    const counts = await query(sql1, info);
    let count = counts[0].count;
    let sql = "select * from user,comment where (ispublic=0 or ispublic=1) and comment.user_id = user.user_id and content_id=? "+
    "order by comment.comment_createtime"
    const result = await query(sql, info);
    data = {
        state: s,
        data: result,
        count: count
    }
    // console.log(data)
    // console.log(result)
    res.send(data);
}
//回复评论
async function webSetReply(req,res){
    let time = Date.now()-8*60*60;
    setNotice(req.user.uid,req.body.user_to,req.user.nickname,req.body.content_id,req.body.content_name,"回复了你",req.body.router);
    let info = {
        reply_id: uuid.v1(),//留言id
        user_id: req.user.uid,//  用户di
        comment_id: req.body.comment_id,//内容id
        reply_content: req.body.comment_content,//内容
        tousernickname: req.body.tousernickname,
        touserid: req.body.touserid,
        createtime: time,//创建时间
        reply_state: 0, //状态
        reply_istop: 0,//是否置顶
        ispublic: 0,//是否显示
    }
    let sql = "insert into reply set ?";
    const result = await query(sql,info);
    data={
        state:s,
        data: {}
    }
    res.send(data);
}
//获取回复列表
async function webGetReplyList(req,res) {
    let info = [req.body.comment_id];
    let sql = "select * from user,reply where  (ispublic=0 or ispublic=1) and  reply.user_id = user.user_id and comment_id=? " +
        "order by reply.createtime asc"
    const result = await query(sql, info);
    data = {
        state: s,
        data: result,
    }
    // console.log(data)
    // console.log(result)
    res.send(data);
}
//获取消息
async function webGetNotice(req,res) {
    let sqlnoticenum = "select count(*) as count from notice where user_to=? and state=0";
    let info = [req.user.uid];
    const count = await query(sqlnoticenum, info);
    let sqlnotice = "select count(*) as count from notice where user_to=?";
    const num = await query(sqlnotice, info);
    let result;
    if (req.body.num == '') {
        let sql = "select * from notice where user_to=? ORDER BY createtime DESC";
        result = await query(sql, info);
    }
    data = {
        state: s,
        data: {
            list: result,
            count: count[0].count,
            num: num[0].count
        }
    }
    // console.log(result)
    res.send(data);
}
//修改消息
async function webChangeNotice(req,res) {
    let info = [];
    let sql = "";
    console.log(req.body.change)
    if(req.body.change === "change"){
        sql="update notice set state=? where user_to=? and notice_id=?";
        info=[1,req.user.uid,req.body.notice_id];
    }
    if(req.body.change === "delete"){
        sql="delete from notice where user_to=? and notice_id=?";
        info=[req.user.uid,req.body.notice_id];
    }
    if(req.body.change === "changeall"){
        sql="update notice set state=? where user_to=?";
        info=[1,req.user.uid];
    }
    if(req.body.change === "deleteall"){
        sql="delete from notice where user_to=?";
        info=[req.user.uid];
    }
    console.log(sql,info)
    const result = await query(sql, info);
    data = {
        state: s,
        data: result
    }
    // console.log(data)
    // console.log(result)
    res.send(data);
}
//获取活动列表
async function webGetActivityList(req,res) {
    let sql1 = "select count(*) as count from activity where (ispublic=0 or ispublic=1)";
    let info1 = [];
    const counts = await query(sql1, info1);
    let count = counts[0].count;
    let pagesize = (req.body.pagesize *= 1);
    let page = (req.body.page - 1) * pagesize;
    let info = [pagesize, page];
    let sql = "select * from activity,user where activity.user_id=user.user_id";
    if (req.body.lable != '') sql = `${sql} and activity.activity_lable='${req.body.lable}'`;//有分类时
    sql = `${sql} and (activity.ispublic=0 or activity.ispublic=1)`;
    sql = `${sql} order by activity.activity_read_num desc limit ? offset ?`;
    const result = await query(sql, info);
    data = {
        state: s,
        data: result,
        count: count
    }
    // console.log(data)
    // console.log(result)
    res.send(data);
}
//活动详情
async function webGetActivityContent(req,res){
    let info = [req.body.id];
    let sql = "select * from activity,user where activity.user_id=user.user_id and activity_id=?";
    const result = await query(sql,info);
    await query("update activity set activity_read_num=activity_read_num+1 where activity_id=?",info);
    if(result.length===0){
        data={
            state:e,
            data:{}
        }
    }else {
        data={
            state:s,
            data:result[0]
        }
    }
    console.log(result);
    res.send(data);
}
//添加jion
async function webSetJoin(req,res){
    let onlyone = [req.body.content_id,req.user.uid];
    let onlyonesql = "select * from joins where content_id=? and user_id=?";
    const resultonlyone = await query(onlyonesql,onlyone);
    if(resultonlyone.length === 0){
        let time = Date.now()-8*60*60;
        setNotice("",req.user.uid,"系统通知",req.body.content_id,req.body.content_name,"你已成功参与",req.body.type);
        setNotice("",req.body.to_userid,"系统通知",req.body.content_id,req.body.content_name,"有一位用户参加了你发布的内容",req.body.type);
        let info = {
            join_id:uuid.v1(),//id
            user_id:req.user.uid,//用户od
            type:req.body.type,
            jions_createtime:time,
            name:req.body.name,//名称
            describe:req.body.describe,//描述
            content_id:req.body.content_id
        }
        let sql = "insert into joins set ?"
        const result = await query(sql,info);
        data = {
            state:s,
            data:{}
        }
    }else {
        data = {
            state:e,
            data:{}
        }
    }
    res.send(data);
}
//获取二手列表
async function webGetOldStuffList(req,res) {
    let sql1 = "select count(*) as count from oldstuff where (ispublic=0 or ispublic=1)";
    if (req.body.lable != '') sql1=`${sql1} and oldstuff_lable='${req.body.lable}'`;
    let info1 = [];
    const counts = await query(sql1, info1);
    let count = counts[0].count;
    let pagesize = (req.body.pagesize *= 1);
    let page = (req.body.page - 1) * pagesize;
    let info = [pagesize, page];
    let sql = "select * from oldstuff,user where oldstuff.user_id=user.user_id";
    if (req.body.lable != '') sql = `${sql} and oldstuff.oldstuff_lable='${req.body.lable}'`;//有分类时
    sql = `${sql} and (oldstuff.ispublic=0 or oldstuff.ispublic=1)`;
    sql = `${sql} order by oldstuff.oldstuff_read_num desc limit ? offset ?`;
    const result = await query(sql, info);
    data = {
        state: s,
        data: result,
        count: count
    }
    // console.log(data)
    // console.log(result)
    res.send(data);
}
//二手详情
async function webGetOldStuffContent(req,res){
    let info = [req.body.id];
    let sql = "select * from oldstuff,user where oldstuff.user_id=user.user_id and oldstuff_id=?";
    const result = await query(sql,info);
    await query("update oldstuff set oldstuff_read_num=oldstuff_read_num+1 where oldstuff_id=?",info);
    if(result.length===0){
        data={
            state:e,
            data:{}
        }
    }else {
        data={
            state:s,
            data:result[0]
        }
    }
    console.log(result);
    res.send(data);
}
//搜索
async function search(req,res){
    let info = [];
    const help = await query(`select * from help,user where help.user_id=user.user_id and help.help_title like "%${req.body.search}%"`,info);
    const activity = await query(`select * from activity where activity_title like "%${req.body.search}%"`,info);
    const job = await query(`select * from job,user where job.user_id=user.user_id and job.job_name like "%${req.body.search}%"`,info);
    const company = await query(`select * from company where company_name like "%${req.body.search}%"`,info);
    const oldstuff = await query(`select * from oldstuff where  oldstuff_name like "%${req.body.search}%"`,info);
    const article = await query(`select * from article,user where article.user_id=user.user_id and (article.article_title like "%${req.body.search}%" or article.article_introduction like "%${req.body.search}%")`,info);
    data = {
        state:s,
        data:{
            help:help,
            activity:activity,
            job:job,
            company:company,
            oldstuff:oldstuff,
            article:article
        }
    }
    res.send(data);
}

module.exports={
    webGetHelpList,
    webGetHelpContent,
    webGetArticleList,
    webGetArticleContent,
    webGetCompanyList,
    webGetCompanyContent,
    webGetJobList,
    webGetJobContent,
    webSetComment,
    webGetCommentList,
    webSetReply,
    webGetReplyList,
    webGetNotice,
    webChangeNotice,
    webGetActivityList,
    webGetActivityContent,
    webSetJoin,
    webGetOldStuffList,
    webGetOldStuffContent,
    search
}
